package com.ducquoc.examreviewdegreea1;

import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;

public class PhoActivity extends Activity {

	ListView lvPho;
	Button btnPho;
	PhoPojo pho = null;
	Button btnClosePopup;
	Button btnCreatePopup;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pho);

		btnPho = (Button) findViewById(R.id.btnPho);

		// Gan du lieu doc tu xml vao listview
		final ArrayList<PhoPojo> arr = new ArrayList<PhoPojo>();

		try {

			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(getAssets().open(
					"list_photograph_content.xml"));

			// Lay ve node goc cua moi bien bao
			NodeList nodeList = doc.getElementsByTagName("photo");
			for (int i = 0; i < nodeList.getLength(); i++) {
				Node node = nodeList.item(i);
				if (node.getNodeType() == Node.ELEMENT_NODE) {
					pho = new PhoPojo();
					Element elm = (Element) node;
					// ID
					NodeList idList = elm.getElementsByTagName("id");
					Element idElement = (Element) idList.item(0);
					pho.setId(idElement.getTextContent().trim());

					// title
					NodeList titleList = elm.getElementsByTagName("title");
					Element titleElement = (Element) titleList.item(0);
					pho.setTitle(titleElement.getTextContent().trim());

					// group
					NodeList groupList = elm.getElementsByTagName("content");
					Element groupElement = (Element) groupList.item(0);
					pho.setContent(groupElement.getTextContent().trim());

					// icon
					NodeList iconList = elm.getElementsByTagName("icon");
					Element iconElement = (Element) iconList.item(0);
					pho.setIcon(iconElement.getTextContent().trim());
					arr.add(pho);
				}
			}
		} catch (Exception e) {

		}

		lvPho = (ListView) findViewById(R.id.lvPho);
		PhoArrayAdapter myArr = new PhoArrayAdapter(PhoActivity.this,
				R.layout.activity_pho_custom_listview, arr);
		lvPho.setAdapter(myArr);

		btnPho.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				initiatePopupWindow();
			}
		});
	}

	private PopupWindow pwindo;

	private void initiatePopupWindow() {
		try {
			// We need to get the instance of the LayoutInflater
			LayoutInflater inflater = (LayoutInflater) PhoActivity.this
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View layout = inflater.inflate(R.layout.activity_pho_popup,
					(ViewGroup) findViewById(R.id.popup_element));
			pwindo = new PopupWindow(layout, 430, 420, true);
			pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);

			btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
			btnClosePopup.setOnClickListener(cancel_button_click_listener);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private OnClickListener cancel_button_click_listener = new OnClickListener() {
		public void onClick(View v) {
			pwindo.dismiss();

		}
	};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}
